package cn.edu.scau.cmi.zenghanxiong.client;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;

//import cn.edu.scau.cmi.zenghanxiong.hibernate.DAO.MajorDAO;
import cn.edu.scau.cmi.zenghanxiong.hibernate.DAO.StudentDAO;
import cn.edu.scau.cmi.zenghanxiong.hibernate.DAO.TeacherDAO;
//import cn.edu.scau.cmi.zenghanxiong.hibernate.domain.Major;
import cn.edu.scau.cmi.zenghanxiong.spring.domain.Student;
import cn.edu.scau.cmi.zenghanxiong.spring.domain.Teacher;
import cn.edu.scau.cmi.zenghanxiong.spring.util.ApplicationContextUtil;

public class SpringHibernateSessionClient {
	/*
	public static void main(String[] args) {
		Major major=new Major();
		major.setName("高级项目管理");
		major.setId(7L);

		newMajor(major);
		listMajors();
	}
		public static void newMajor(Major major) {
//			MajorDAO majorDAO=new MajorDAO();
			
			ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
			MajorDAO majorDAO=(MajorDAO) ac.getBean("majorDAO");
			
//			要将数据保存到数据库，或者使用transaction，或者使用session的fulsh方法
			Transaction transaction = majorDAO.getSession().beginTransaction();
			majorDAO.save(major);
			transaction.commit();
//			(3) 或者不适用事务，使用flush从缓冲区中将数据持久化到数据库中
//			majorDAO.getSession().flush();
		}

	private static void listMajors() {
//		MajorDAO majorDAO=new MajorDAO();
		
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		MajorDAO majorDAO=(MajorDAO) ac.getBean("majorDAO");
		List Majors = majorDAO.findAll();
		Iterator<?> MajorIterator = Majors.iterator();
		while(MajorIterator.hasNext()){
			Major Major = (Major) MajorIterator.next();
			System.out.println("调用DAO后的专业名是："+Major.getName()+"，调用DAO后的专业ID号是："+Major.getId());
		}
	}
*/
	public static void main(String[] args) {
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		StudentDAO stu=(StudentDAO) ac.getBean("zhangsan");
		List stus =stu.findAll();
		Iterator<?> StudentIterator = stus.iterator();
		Student student=(Student) StudentIterator.next();
		System.out.println(student.getName()+"的毕业设计指导导师是："+student.getTutor().getName());
	}

}
